﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMS.Service.UnitOfWork;
using CMS.Data;
using System.Data;

namespace CMS.Service.Repository
{
    public class CustomerRepository : RepositoryBase<Customer>
    {
        public CustomerRepository()
            : base()
        {
        }

        public CustomerRepository(WorkUnit unit)
            : base(unit)
        {
        }

        public override int CreateOrUpdate(Customer entity)
        {
            try
            {
                Context.Entry(entity).State = entity.Id == 0 ?
                                       EntityState.Added :
                                       EntityState.Modified;
                Context.SaveChanges();
                return entity.Id;
            }
            catch { return 0; }
        }

        public override string Delete(int entityId)
        {
            try
            {
                var entity = Context.Customers.SingleOrDefault(o => o.Id == entityId);
                Context.Customers.Remove(entity);
                Context.SaveChanges();
                return "success";
            }
            catch { return "error"; }
        }

        public override Customer GetById(int entityId)
        {
            try
            {
                return Context.Customers.SingleOrDefault(p => p.Id == entityId);
            }
            catch { return null; }
        }

        public override List<Customer> List()
        {
            try
            {
                return Context.Customers.ToList();
            }
            catch { return null; }
        }

        public override List<Customer> GetTableListById(int entityId)
        {
            throw new NotImplementedException();
        }

        public List<Customer> Search(string keys, int type, int active, DateTime created_on_from, DateTime created_on_to)
        {
            try
            {
                var query = Context.Customers.Where(x => 1 == 1);

                if (!string.IsNullOrEmpty(keys))
                {
                    query = query.Where(x => x.Name.Contains(keys) || x.Phone.Contains(keys) || x.Addr.Contains(keys));
                }

                if (type > 0)
                {
                    query = query.Where(x => x.Type == type);
                }

                if (active > 0)
                {
                    query = query.Where(x => x.Active == (active == 1 ? true : false));
                }

                if (created_on_from > DateTime.MinValue)
                {
                    query = query.Where(x => x.CreatedOn >= created_on_from);
                }

                if (created_on_to > DateTime.MinValue)
                {
                    query = query.Where(x => x.CreatedOn <= created_on_to);
                }
                
                return query.ToList();
            }
            catch
            {
                return null;
            }
        }
    }
}
